package com.example.demo.service;

import com.example.demo.dao.*;
import com.example.demo.po.*;
import com.example.demo.util.DateUtil;
import com.example.demo.util.PageUtils;
import com.example.demo.util.SeqFactory;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

@Service
public class TBizOrderService {
    @Autowired
    TBizOrderDAO tBizOrderDAO;
    @Autowired
    TBizOrderArtDAO tBizOrderArtDAO;
    @Autowired
    WxPayRecordDAO wxPayRecordDAO;
    @Autowired
    TBizArtDAO tBizArtDAO;
    @Autowired
    TBizShoppingDAO tBizShoppingDAO;
    @Autowired
    WxUserAddressDAO wxUserAddressDAO;

    /**
     * 分页查询接口
     * 这里统一封装了分页请求和结果，避免直接引入具体框架的分页对象, 如MyBatis或JPA的分页对象
     * 从而避免因为替换ORM框架而导致服务层、控制层的分页接口也需要变动的情况，替换ORM框架也不会
     * 影响服务层以上的分页接口，起到了解耦的作用
     * @param pageRequest 自定义，统一分页查询请求
     * @return PageResult 自定义，统一分页查询结果
     */
    public PageResult findByPage(TBizOrder orderInfo, PageRequest pageRequest){
        int pageNum = pageRequest.getPageNum();
        int pageSize = pageRequest.getPageSize();
        PageHelper.startPage(pageNum, pageSize);
        List<TBizOrder> list = tBizOrderDAO.list(orderInfo);
        PageResult pageResult = PageUtils.getPageResult(pageRequest, new PageInfo<TBizOrder>(list));
        return pageResult;
    }


    /**
     * 根据用户ID查询订单信息
     * @param orderInfo
     * @param pageRequest
     * @return
     */
    public PageResult findByPages(TBizOrder orderInfo, PageRequest pageRequest){
        int pageNum = pageRequest.getPageNum();
        int pageSize = pageRequest.getPageSize();
        PageHelper.startPage(pageNum, pageSize);
        String type = orderInfo.getType();
        PageResult pageResult = null;
        if("1".equals(type.trim())){
            List<TBizOrder> list = tBizOrderDAO.list(orderInfo);
            pageResult = PageUtils.getPageResult(pageRequest, new PageInfo<TBizOrder>(list));
            List<TBizOrder> list1 = (List<TBizOrder>) pageResult.getContent();
            if(list1!=null&&list1.size()>0){
                List<TBizOrderArt> resultList = new ArrayList<>();
                for(TBizOrder order :list1){
                    TBizOrderArt oa = new TBizOrderArt();
                    oa.setOrderid(order.getOrderid());
                    List<TBizArt> artList = tBizArtDAO.findArtByOrder(order.getOrderid());
                    oa.setArtList(artList);
                    oa.setOrderInfo(order);
                    resultList.add(oa);
                }
                pageResult.setContent(resultList);
            }
        }else{
            List<TBizOrderArt> list = tBizOrderArtDAO.findAucByOrder(orderInfo);
            pageResult = PageUtils.getPageResult(pageRequest, new PageInfo<TBizOrderArt>(list));
        }
        return pageResult;
    }


    /**
     * 查询订单详情
     * @param orderInfo
     * @return
     */
    public TBizOrderArt findInfoByOrderID(TBizOrder orderInfo){
        TBizOrderArt oa =null;
        if("1".equals(orderInfo.getType())){
            TBizOrder order=tBizOrderDAO.findById(orderInfo.getOrderid());
            oa = new TBizOrderArt();
            oa.setOrderInfo(orderInfo);
            if(order.getAddress()!=null && !"".equals(order.getAddress())){
                String addressId = order.getAddress();
                WxUserAddress address = wxUserAddressDAO.findById(Integer.valueOf(addressId));
                oa.setAddressInfo(address);
            }
            List<TBizArt> artList = tBizArtDAO.findArtByOrder(order.getOrderid());
            oa.setArtList(artList);
        }else {
            oa = tBizOrderArtDAO.findBytype2(orderInfo);
        }
        return oa;
    }


    @Transactional
    public boolean Update(TBizOrder orderInfo){
        int i = tBizOrderDAO.update(orderInfo);
        if(i>0)
            return true;
        else
            return false;
    }


    @Transactional
    public boolean createOrder(TBizOrder orderInfo,TBizOrderArt oa, HttpServletRequest request){
        orderInfo.setCreatetime(DateUtil.formatDate(new Date()));
        int i = tBizOrderDAO.save(orderInfo);
        if(i>0){
            int y = tBizOrderArtDAO.save(oa);
            if(y>0)
                return true;
            else
                return false;
        }else
            return false;
    }

    @Transactional
    public boolean createShoppingOrder(TBizOrder orderInfo,List<TBizOrderArt> oaList,List<String> shopIds, HttpServletRequest request){
        orderInfo.setCreatetime(DateUtil.formatDate(new Date()));
        int i = tBizOrderDAO.save(orderInfo);
        if(i>0){
            int y = tBizOrderArtDAO.insertList(oaList);
            if(y>0){
              int z = tBizShoppingDAO.del(shopIds);
              if(z>0)
                return true;
              else
                  return false;
            }
            else
                return false;
        }else
            return false;
    }


    @Transactional
    public boolean payTrue(TBizOrder orderInfo,WxPayRecord wpr){
        int i = tBizOrderDAO.update(orderInfo);

        if(i>0) {
            int y = wxPayRecordDAO.save(wpr);
            if(y>0)
                return true;
            else
                return false;
        }else
            return false;
    }



    @Transactional
    public boolean saveInfo(TBizOrder orderInfo,String artCodes){
        String orderId ="O"+ SeqFactory.getNewSequenceAlone();
        orderInfo.setOrderid(orderId);
        orderInfo.setCreatetime(DateUtil.formatDate(new Date()));
        int i = tBizOrderDAO.save(orderInfo);
        if(i>0) {
            List<TBizOrderArt> list = new ArrayList<>();
            if(artCodes.contains(",")){
                String[] artCode = artCodes.split(",");
                for (String a : artCode) {
                    TBizOrderArt oa = new TBizOrderArt();
                    oa.setOrderid(orderId);
                    oa.setArtcode(a);
                    oa.setNumber(1);
                    list.add(oa);
                }
            } else{
                TBizOrderArt oa = new TBizOrderArt();
                oa.setOrderid(orderId);
                oa.setArtcode(artCodes);
                oa.setNumber(1);
                list.add(oa);
            }
            tBizOrderArtDAO.insertList(list);
            return true;
        }
        return false;
    }


    public TBizOrder findById(String orderId){
        return  tBizOrderDAO.findById(orderId);
    }

    /**
     * 判断用户为某个拍卖会交了定金
     * @param openid 用户唯一标识
     * @param aucid 拍卖会ID
     * @return
     */
    public String queryPay(String openid,String aucid){
        Map<String,String> map = new HashMap<>();
        map.put("acutionId",aucid);
        map.put("userId",openid);
        List<String> re = tBizOrderDAO.queryPay(map);
        if(re != null && re.size()>0)
            return "success";
        else
            return "fail";
    }

}
